package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.MealCalendar;
import com.neusoft.SPNursingHome.po.MealCalendarWithFood;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MealCalendarDao extends BaseMapper<MealCalendar> {

    @Select("""
      <script>
      SELECT 
        mc.meal_calendar_id      AS mealCalendarId,
        mc.meal_calendar_date    AS mealCalendarDate,
        mc.meal_calendar_time    AS mealCalendarTime,
        mc.meal_calendar_status  AS mealCalendarStatus,
        f.food_id                AS foodId,
        f.food_name              AS foodName,
        f.food_type              AS foodType
      FROM meal_calendar mc
      LEFT JOIN food f
        ON mc.food_id = f.food_id
      <where>
       AND mc.meal_calendar_status = '启用'
        <if test="mealCalendarDate != null and mealCalendarDate != ''">
          AND mc.meal_calendar_date LIKE CONCAT('%', #{mealCalendarDate}, '%')
        </if>
        <if test="mealCalendarTime != null and mealCalendarTime != ''">
          AND mc.meal_calendar_time LIKE CONCAT('%', #{mealCalendarTime}, '%')
        </if>
        <if test="foodName != null and foodName != ''">
          AND f.food_name LIKE CONCAT('%', #{foodName}, '%')
        </if>
        <if test="foodType != null and foodType != ''">
          AND f.food_type LIKE CONCAT('%', #{foodType}, '%')
        </if>
      </where>
      ORDER BY
        FIELD(mc.meal_calendar_date,
              '周一','周二','周三','周四','周五','周六','周日'),
        FIELD(mc.meal_calendar_time,
              '早','午','晚')
      </script>
    """)
    IPage<MealCalendarWithFood> selectWithFoodName(
            IPage<MealCalendarWithFood> page,
            @Param("mealCalendarDate") String mealCalendarDate,
            @Param("mealCalendarTime") String mealCalendarTime,
            @Param("foodName") String foodName,
            @Param("foodType") String foodType
    );
}
